<?php
class Bloc
{
    /**
     * Variable de " globale"
     * */
    protected $db;                          // Liaison base de donnee
    protected $lang;                        // Fichier lanh
    protected $session;                     // Class session
    protected $setting;                     // Tableau des setting
    protected $smarty;                      // Class smarty pour affichage
    protected $cache;                       // Class lite pour acceder au cache
    
    public $bloc;                            // Contient le contenu du bloc
    
    /**
     * @desc Construit la class
     * */
    public function __construct($paramClass)
    {
        if(is_array($paramClass))
        {
            foreach($paramClass as $key => $value)
            {
                $this->$key = $value;
            } 
        }     
    }
    
    public function getBloc($bloc_id)
    {
        $this->bloc = $this->db->sql_query("SELECT * FROM immo_bloc WHERE id = '" . $bloc_id . "' ", 'ARRAY');
    }
    
    /**
     * @desc Affiche les message d action
     * @param $text : message a afficheer
     * @param $type : definie l encadrement et la couleur
     * @return void
     * */
    public function printErrorMessage($text, $type = 'success')
    {
        if($this->_printMessage === true)
        {
            $this->smarty->assign('error_msg', true);
            $this->smarty->assign('error_class', 'error_success');
            $this->smarty->assign('error_image', 'comment_ok');
            $this->smarty->assign('error_message',$text);
            $this->smarty->assign('url_site',$this->setting['url_site']);   
        }        
    }
    
    /**
     * @desc retourne la liste des blocs
     * */
    public function getAll()
    {
        $Sql = $this->db->sql_query("SELECT * FROM immo_bloc ORDER BY emplacement, ordre"); 
        
        $blocs = array();
        
        while($bloc = $this->db->sql_fetcharray($Sql))
        {
            $bloc['action'] = '<a href="bloc_edition.php?bloc=' . $bloc['id'] . '"><img src="images/edit.png" style="border:none; width:20px; height:20px;" /></a>&nbsp;&nbsp;<a href="javascript:delbloc(\'' . $bloc['id'] . '\');"><img src="images/del.png" style="border:none; width:20px; height:20px;" /></a>';  
            
            if( $bloc['contenu'] != '') 
                $bloc['plugins'] = 'HTML';
            
            array_push($blocs, $bloc);            
        }   
        
        $this->smarty->assign('blocs', $blocs);
    }
    
    /**
     * @desc Affiche la liste des blocs disponible
     * */
    public function getDisponible()
    {
        $Sql = $this->db->sql_query("SELECT * FROM immo_bloc_disponible ORDER BY nom, plugins");
        
        $blocs = array();
        
        while($bloc = $this->db->sql_fetcharray($Sql))
        {
            $bloc['action'] = '<a href="bloc_active_disponible.php?bloc=' . $bloc['id'] . '"><img src="images/add.png" style="border:none; width:20px; height:20px;" /></a>';
            
            array_push($blocs, $bloc);
        }    
        
        $this->smarty->assign('blocs_disponible', $blocs);
    }
    
    /**
     * @desc Recuperer les informations d un bloc disponible
     * @return void
     * */
     public function getBlocDisponible($bloc_id)
     {
        $this->bloc = $this->db->sql_query("SELECT * FROM immo_bloc_disponible WHERE id = '" . $bloc_id ."'", 'ARRAY');
     }
     
     public function getEmplacementArray()
     {
        $emplacement =  array( 
            '1' =>  $this->lang['Haut_gauche'],
            '2' =>  $this->lang['Haut_droite'],
            '3' =>  $this->lang['Haut_centre'],
            '4' =>  $this->lang['Gauche'],
            '5' =>  $this->lang['Droite'],
            '6' =>  $this->lang['Bas_gauche'],
            '7' =>  $this->lang['Bas_droite'],
            '8' =>  $this->lang['Bas_centre'],
        );  
        
        return $emplacement;
     }
    
    /**
     * @desc supprime le bloc de la base selon son id
     * @id identifiant du bloc a supprime
     * */
    function delete($id)
    {
        global $lang, $template, $db;
        if($id == '' || !is_numeric($id)) redirect('bloc.php');
        $db->sql_query("DELETE FROM immo_bloc WHERE id = '". $id ."'");
        $template->assign_vars(array(
        'ERROR_MSG'	=> true,
        'ERRORH_CLASS'	=> 'error_success',
        'ERRORH_IMG'	=>	'comment_ok',
        'V_ERROR_TXT'	=>	$lang['Bloc_supprime'])); 
    }
    /**
     * @desc Modifie un bloc HTML
     * @param ils sont recuperes par la fonction retrieve, var $_POST
     * */
    function edition_html()
    {
        global $template, $db, $lang;
        if(!$id = retrieve('POST','id',true)) redirect('bloc.php?error=bloc_id');
        if(!$name = retrieve('POST','titre',true)) redirect('bloc.php?error=bloc_titre');
        if(!$contenu = retrieve('POST','contenu',true)) redirect('bloc.php?error=bloc_contenu');
        $emplacement = retrieve('POST','emplacement');
        $ordre = retrieve('POST','ordre');
        if($ordre == '' || !is_numeric($ordre)) $ordre = 0;   
        $QueryArray = array(
        'name'  =>  $name,
        'fichier'   =>  'bloc/bloc_html.php',
        'ordre' =>  $ordre,
        'actif' =>  '1',
        'emplacement'   =>  $emplacement,
        'plugins' => '',
        'contenu'   =>  addslashes($contenu)
        );
        $db->sql_query(CreateQueryUpdate($QueryArray, 'immo_bloc'," id = '". $id ."' "));
        $template->assign_vars(array(
        'ERROR_MSG'	=> true,
        'ERRORH_CLASS'	=> 'error_success',
        'ERRORH_IMG'	=>	'comment_ok',
        'V_ERROR_TXT'	=>	$lang['Bloc_modifie']));        
    }
    /**
     * @desc Modifie un bloc pre defini
     * @param ils sont recuperes par la fonction retrieve, var $_POST
     * */
    function edition()
    {
        global $template, $db, $lang;
        if(!$id = retrieve('POST','id',true)) redirect('bloc.php?error=bloc_id');
        if(!$name = retrieve('POST','titre',true)) redirect('bloc.php?error=bloc_titre');
        $emplacement = retrieve('POST','emplacement');
        $ordre = retrieve('POST','ordre');
        if($ordre == '' || !is_numeric($ordre)) $ordre = 0;   
        $QueryArray = array(
        'name'  =>  $name,
        'ordre' =>  $ordre,
        'actif' =>  '1',
        'emplacement'   =>  $emplacement,
        'plugins' => '',
        );
        $db->sql_query(CreateQueryUpdate($QueryArray, 'immo_bloc'," id = '". $id ."' "));
        $template->assign_vars(array(
        'ERROR_MSG'	=> true,
        'ERRORH_CLASS'	=> 'error_success',
        'ERRORH_IMG'	=>	'comment_ok',
        'V_ERROR_TXT'	=>	$lang['Bloc_modifie']));  
    }
}